﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Permissions;
//using PastePicture;
using Petroleum.Tool;

namespace Petroleum.UC.Public
{
    /// <summary>
    /// 图片选择控件
    /// </summary>
    public partial class PictureTool : UserControl
    {
        public string ImgUrl = "";
        Form form = null;
        public PictureTool()
        {
            InitializeComponent();
            this.btn.BtnClick += new System.EventHandler(this.btnImg_BtnClick);
            this.btncopy.BtnClick += new System.EventHandler(this.PictureTool_Click);
            this.picImg.DoubleClick += PicImg_DoubleClick;
        }
        private void PicImg_DoubleClick(object sender, EventArgs e)
        {

        }
        private void PictureTool_Click(object sender, EventArgs e)
        {
            GetShortImg();
            ImgUrl = picImg.ImageLocation;
        }
        public PictureTool(Form form)
        {
            InitializeComponent();
            this.form = form;
            this.btn.BtnClick += new System.EventHandler(this.btnImg_BtnClick);
            this.btncopy.BtnClick += new System.EventHandler(PictureTool_Click);
        }
        public void Initialization(string Title)
        {
            labTitle.Text = Title;
        }
        private void btnImg_BtnClick(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            DialogResult result = fileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                picImg.Image = Image.FromFile(fileDialog.FileName);
                ImgUrl = fileDialog.FileName;
            }
        }
        /// <summary>
        /// 监视Windows消息
        /// 重载WndProc方法，用于实现热键响应
        /// </summary>
        /// <param name="m"></param>
        //[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        //protected override void WndProc(ref Message m)
        //{
        //    const int WM_HOTKEY = 0x0312;
        //    //按快捷键
        //    switch (m.Msg)
        //    {
        //        case WM_HOTKEY:
        //            switch (m.WParam.ToInt32())
        //            {
        //                case 103:
        //                    LogHelper.loginfo.Info("热键捕捉" + m.Msg);
        //                    //复制图片
        //                    ImgUrl = picImg.ImageLocation;
        //                    break;
        //                case 104:
        //                    //粘贴图片
        //                    GetShortImg();
        //                    ImgUrl = picImg.ImageLocation;
        //                    break;

        //            }
        //            break;
        //    }
        //    base.WndProc(ref m);
        //}
        void GetShortImg()
        {
            try
            {
                IDataObject iData = Clipboard.GetDataObject();
                if (iData.GetDataPresent(DataFormats.FileDrop))
                    picImg.ImageLocation = Array.ConvertAll<object, string>((object[])iData.GetData(DataFormats.FileDrop), Convert.ToString)[0];
                //if (iData.GetDataPresent(DataFormats.Bitmap))
                //    picImg.Image = (Bitmap)iData.GetData(DataFormats.Bitmap, true);
            }
            catch (Exception ex)
            {

            }
        }
    }
}
